package com.hghivln.blog.controller;


import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.entity.Blog;
import com.hghivln.blog.pojo.dto.BlogDto;
import com.hghivln.blog.pojo.vo.BlogVo;
import com.hghivln.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-09-11
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 添加博客
     */
    @PostMapping("save")
    public Result<?> addBlog(@RequestBody BlogDto blogDto) {
        try {
            blogService.saveBlog(blogDto);
            return new Result<>("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("保存失败");
        }
    }

    /**
     * 更新
     */
    @PutMapping("update")
    public Result<?> update(@RequestBody BlogDto blogDto) {
        try {
            blogService.updateBlog(blogDto);
            return new Result<>("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("修改失败");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping( "/delete/{id}")
    public Result<?> deleteById(@PathVariable Long id) {
        try {
            blogService.deleteById(id);
            return new Result<>("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("删除失败");
        }
    }

    /**
     * 分页查询
     */
    @PostMapping("getByPage")
    public Result<?> loadAllLog(@RequestBody BlogDto blogDto) {
        return blogService.loadAllLog(blogDto);
    }

    /**
     * 根据分类名称 查询博客
     */
    @PostMapping("getBlogByType")
    public Result<?> getBlogByType(@RequestBody BlogDto blogDto) {
        return blogService.getBlogByType(blogDto);
    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public Result<BlogVo> getInfo(@PathVariable Long id) {
        BlogVo blogVo = blogService.getInfo(id);
        return new Result<>(blogVo);
    }

    /**
     * 查询用于修改的回显数据
     */
    @GetMapping("/getUpdate/{id}")
    public Result<Blog> getUpdate(@PathVariable Long id) {
        Blog blog = blogService.getUpdate(id);
        return new Result<>(blog);
    }

    /**
     * 获取时间轴
     */
    @GetMapping("/getTimeLine/{index}")
    public Result<?> getTimeLine(@PathVariable Integer index) {
        List<Blog> list = blogService.getTimeLine(index);
        return new Result<>(list);
    }

    /**
     * 获取点赞数阅读数评论数收藏数
     */
    @GetMapping("/getMeta/{blogId}")
    public Result<?> getMeta(@PathVariable Long blogId) {
        return blogService.getMeta(blogId);
    }

}
